/**
 * JAVACC DEMO 1.0
 * @copy right iussoft company All rights reserved.
 * @Package com.apache.passport.service.plugins  
 */
package com.apache.passport.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.Validator;
import com.apache.database.db.IDao;
import com.apache.passport.common.Native2AsciiUtils;
import com.apache.passport.common.PassportHelper;
import com.apache.passport.entity.UctUser;
import com.apache.passport.manager.SynInfoManager;
import com.apache.passport.manager.UctUserManager;

/**
 * description: socket,webservice,http,https注册插件 
 */
public class UserOwRegPlugin implements PluginConnector {

	private ParamsVo threadVo = null;
	private UctUserManager uctUserManager;

	public void setUctUserManager(UctUserManager uctUserManager) {
		this.uctUserManager = uctUserManager;
	}

	private SynInfoManager synInfoManager;

	public void setSynInfoManager(SynInfoManager synInfoManager) {
		this.synInfoManager = synInfoManager;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();
		result.setResult("true");
		UctUser uctUser = new UctUser();
		String userEname = (String) vo.getParams("userEname");
		if (Validator.isNull(userEname)) {
			result.setEntity("F");
			result.setMessage("用户名不能为空");
			return result;
		}

		String userCname = (String) vo.getParams("userCname");
		if (Validator.isNull(userCname)) {
			result.setEntity("F");
			result.setMessage("中文名不能为空");
			return result;
		}

		String osType = PassportHelper.getInstance().getSysOs();
		if ("w".equals(osType)) {
			userCname = Native2AsciiUtils.getInstance().ascii2Native(userCname);
		} else if ("u".equals(osType)) {
			userCname = Native2AsciiUtils.getInstance().ascii2Native(userCname);
		}

		String email = (String) vo.getParams("email");
		if (Validator.isNull(email)) {
			result.setEntity("F");
			result.setMessage("邮箱不能为空");
			return result;
		}
		String mobile = (String) vo.getParams("mobile");
		if (Validator.isNull(mobile)) {
			result.setEntity("F");
			result.setMessage("手机号不能为空");
			return result;
		}
		String userPass = (String) vo.getParams("userPass");
		if (Validator.isNull(userPass)) {
			result.setEntity("F");
			result.setMessage("密码不能为空");
			return result;
		}
		//md5加密
		//userPass = MD5Utils.MD5(userPass);
		//vo.setParams("userPass", userPass);
		//验证用户名唯一
		vo.setMethodKey("chkEname");
		result = (ResultEntity) uctUserManager.execute(vo);
		if (!"T".equals(result.getEntity())) {
			return result;
		}

		//验证邮箱唯一
		vo.setMethodKey("chkEmail");
		result = (ResultEntity) uctUserManager.execute(vo);
		if (!"T".equals(result.getEntity())) {
			return result;
		}
		//验证手机号唯一
		vo.setMethodKey("chkMobile");
		result = (ResultEntity) uctUserManager.execute(vo);
		if (!"T".equals(result.getEntity())) {
			return result;
		}
		//存到对象中

		uctUser.setUserEname(userEname);
		uctUser.setUserCname(userCname);
		uctUser.setUserPass(userPass);
		uctUser.setMobile(mobile);
		uctUser.setEmail(email);
		//2015年1月28日16:01:09 添加 否则uc无法查看
		uctUser.setDelStatus("F");
		// 2015年3月10日20:21:31 修改 0为停用 ,1为启用
		uctUser.setUserStatus(1);
		vo.setObj(uctUser);
		String userId = uctUserManager.saveInfo(vo);
		if (!Validator.isNull(userId)) {
			result.setEntity("T");
			result.setMessage("用户注册成功");
			threadVo = vo;
			//信息同步
			threadVo.setParams("user", uctUser);
			Thread th = new Thread() {
				public void run() {
					threadVo.setMethodKey("synInfoPlugin");
					synInfoManager.execute(threadVo);
				};
			};
			th.start();
			return result;
		}
		return result;
	}
}
